#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1010, M = 100010;
int h[N], e[M], ne[M], idx;
bool st[N];
int n, m;
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int bfs(int u)
{
    queue<int> q;
    q.push(u);
    int k = m, ans = 0;
    while (k--)
    {
        int sz = q.size();
        while (sz--)
        {
            int t = q.front();
            q.pop();
            for (int i = h[t]; ~i; i = ne[i])
            {
                int j = e[i];
                if (!st[j])
                {
                    st[j] = true;
                    ans++;
                    q.push(j);
                }
            }
        }
    }
    return ans;
}
int main()
{
    cin >> n >> m;
    int cnt = 0;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= n; i++)
    {
        cin >> cnt;
        while (cnt--)
        {
            int b;
            cin >> b;
            add(b, i);
        }
    }
    cin >> cnt;
    while (cnt--)
    {
        int u;
        cin >> u;
        memset(st, false, sizeof st);
        st[u] = true;
        cout << bfs(u) << endl;
    }
    return 0;
}